#include<stdio.h>
#include<string.h>
#include "../include/block.h"
#include "../include/fileDescriptor.h"
#include "../include/freeList.h"
#include "../include/headerFile.h" 
#define FILESIZE 100

void create_vfs(char nameOfFile[50], long size)
{
	
	//Variables used
	char temp[FILESIZE];
	int i=0;	
	char filepath[100]="/home/anuj/Desktop/vfs31/";
	
	//Concatenate the filename to path
	strcat(filepath, nameOfFile);
	
	//Creating object of the various structure type
	struct mainHeader mHeader;
	struct FileDescriptor fDescriptor;
	struct FreeList fList; 
	
	//Declaring FILE ptr
	FILE *fp;
		
	
	//Creating Large Binary File
	fp=fopen(filepath,"wb");
	if(fp==NULL)
	{
		//System runs out of memory
		printf("Operation terminated! Unable to allocate Memory.\n"); 
	}
	else
	{
		//Memory allocated
		fseek(fp,size,SEEK_SET);
		fwrite(temp,1,1,fp);
	}
	
	//Initialzing META HEADER (mainHeader) structure 
	strcpy(mHeader.fileSystemLabel,"VFS");
	mHeader.noOfFileDescriptors=0;
	mHeader.maxNoOfFileDiscriptors=100;
	
	//Writing mainheader structure into memory
	fseek(fp,0,SEEK_SET);
	fwrite(&mHeader,sizeof(struct mainHeader),1,fp);
	
	//Creating Array of File Descriptors
	struct FileDescriptor fDescriptorArray[20];
	//memset(fDescriptorArray, 0x00, sizeof(fDescriptorArray));// for initializing the array of fds' to 0
	fseek(fp,sizeof(struct mainHeader),SEEK_SET);
	fwrite(&fDescriptorArray,sizeof(struct FileDescriptor),20,fp);
	
	//Creating blocks in memory
	struct Block block[100];
	for(i=0;i<100;i++)
	{
		strcpy(block[i].FixedSizedBuffer,"Block");
	}	
	fseek(fp,sizeof(struct mainHeader)+(sizeof(struct FileDescriptor)*20),SEEK_SET);
	fwrite(&block,sizeof(struct Block),100,fp);
	
	
	//Reading and displaying the contents of file
	//Reading Label
	char m[100];
	FILE *fpRead=fopen(filepath,"rb");
	fread(m,1,12,fpRead);
	printf("\nLabel:  %s \n",m);
	
	//Reading Block
	struct Block b;
	fpRead=fopen(filepath,"rb");
	fseek(fpRead,sizeof(struct mainHeader)+(sizeof(struct FileDescriptor)*20),SEEK_SET);
	fread(&b,sizeof(struct Block),1,fpRead);
	printf("\nBlock:  %s \n",b.FixedSizedBuffer);
	printf("\nEnd \n");	
}

void main()
{
	char nameOfFile[50];
	printf("Enter the name of file you wish to create:  ");
	scanf("%s", nameOfFile);
	long size = 1000000;
	create_vfs(nameOfFile, size);
}
